/**
 * 学生模块API
 */

import axios from './request';

/**
 * 是否为空
 * @param {*} value 
 * @returns 
 */
const isEmpty = value => null === value || undefined === value;

/**
 * 添加学生
 */
export const AddStudents = obj => {
    const { avatar, ...rest } = obj;
    const form = new FormData();
    avatar && form.append('file', avatar);
    Object.entries(rest).forEach(([key, value]) => !isEmpty(value) && form.append(key, value));
    return axios.post('/students/add', form, {
        headers: {
            'content-type': 'multipart/form-data'
        }
    });
}

/**
 * 搜索学生列表
 * @returns 
 */
export const Search = obj => axios.get('/students/search', { params: obj });

/**
 * 修改学生
 * @returns 
 */
export const UpdateStudents = student => {
    const { avatar, ...rest } = student;
    const form = new FormData();
    avatar && form.append('file', avatar);
    Object.entries(rest).forEach(([key, value]) => !isEmpty(value) && form.append(key, value));
    return axios.put('/students/update', form, {
        headers: {
            'content-type': 'multipart/form-data'
        }
    });
}

/**
 * 变更状态
 * @param {*} obj 要变更状态的数据
 * @returns 
 */
export const ChangeStatus = obj => axios.put('/students/change', obj);

/**
 * 更改学生档案
 * @param {*} obj 
 * @returns 
 */
export const ChangeRecord = obj => axios.put('/students/record', obj);

/**
 * 添加学生紧急联系人
 * @param {*} emergency 紧急联系人信息
 * @returns 
 */
export const AddEmergency = emergency => axios.post('/studentEmergency/add', emergency);

/**
 * 修改学生紧急联系人
 * @param {*} emergency 紧急联系人信息
 * @returns 
 */
export const UpdateEmergency = emergency => axios.put('/studentEmergency/update', emergency);

/**
 * 删除学生紧急联系人
 * @param {*} id 紧急联系人id
 * @returns 
 */
export const DeleteEmergency = id => axios.delete('/studentEmergency/delete/' + id);

/**
 * 获取学生紧急联系人
 * @param {*} id 紧急联系人id
 * @returns 
 */
export const GetEmergencyList = id => axios.get('/studentEmergency/list/' + id);

/**
 * 搜索学生档案
 * @param {*} obj 查询对象
 * @returns 
 */
export const SearchArchives = obj => axios.get('/students/searchArchives', { params: obj });

/**
 * 添加费用
 * @param {*} obj 
 * @returns 
 */
export const AddFee = obj => axios.post('/students/addFee', obj);

/**
 * 搜索学生费用
 * @param {*} obj 查询对象
 * @returns 
 */
export const SearchFee = obj => axios.get('/students/searchFee', { params: obj });